#!/bin/bash
#作 者: dugigeek@2362317758@qq.com
#日 期: 20200111
#功 能：创建完整的dugicaf嵌入式系统自动化编译框架

set -e
export logtag=000

#>>-----------------------------------------------------------------------------------
#顶层项目目录
export PROJECT_TOPDIR=$PWD
#顶层当前配置文件
export CURCFG_FILE=${PROJECT_TOPDIR}/.curconfig.sh
#-------------------------------------------------------------------------------------

#>>env--------------------------------------------------------------------------------
#设置颜色函数
source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.000.colorsetup.sh
#导入公共变量
source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.001.envsetup_common.sh
#导入平台变量
source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.002.envsetup_platform.sh
#-------------------------------------------------------------------------------------


export CAFKIT_PATH=~/CAFKIT

export cafgit_site="https://gitee.com/dugiiot/dugicaf.git"
export cafgit_root=${CAFKIT_PATH}/src
export cafgit_name=dugicaf
export cafgit_path=${cafgit_root}/${cafgit_name}

#export cafloc_root=${PROJECT_TOPDIR}/.CAFKIT/src
export cafloc_root=${CAFKIT_PATH}/src
export cafloc_name=cafloc
export cafloc_path=${cafloc_root}/${cafloc_name}

show_usage()
{ 
	msglog "--------------------------------------------------"
	msglog "使用说明:"
	msglog "$0 01   查看CAF的框架说明"
	msglog "$0 02   克隆dugicaf到: ${cafgit_path}"
	msglog "$0 03   同步dugicaf到: ${cafgit_path}"
	msglog "$0 04   递交dugicaf到: ${cafgit_path}"
	msglog "$0 05   推送dugicaf到: ${cafgit_site}"
	msglog "$0 06   聚集生成: ${cafloc_path}"
	msglog "$0 07   拷贝${cafloc_path}到${cafgit_path}"
	msglog "=================================================="
}

show_end()
{
	msglog "正常结束 [DONE]"
	msglog "=================================================="
}

gether_cafloc()
{
	msglog "--------------------------------------------------"
	msglog "聚集cafloc [START]"
	local curdate=$(date +%y%m%d%H%M)
	if [ -d ${cafloc_path} ]; then
		mv 	${cafloc_path} 	${cafloc_path}.${curdate}
		msglog "\n旧目录:\n${cafloc_path}\n命名为:\n${cafloc_path}.${curdate} [OK]"
	fi
	mkdir -p ${cafloc_path}
	msglog "新建目录：${cafloc_path} [OK]"
	cp ${PROJECT_TOPDIR}/*.sh      ${cafloc_path}/
	cp ${PROJECT_TOPDIR}/*.cmd     ${cafloc_path}/
	cp ${PROJECT_TOPDIR}/a.OEMKIT  ${cafloc_path}/ -rf
	
	mkdir -p ${cafloc_path}/b.GCCKIT
	cp ${GCCKIT_TOPDIR}/envsetup_gcckit.sh ${cafloc_path}/b.GCCKIT/

	mkdir -p ${cafloc_path}/c.SYSKIT
	for plat in $(ls ${SYSKIT_TOPDIR})
	do
		mkdir -p ${cafloc_path}/c.SYSKIT/${plat}
		cp ${SYSKIT_TOPDIR}/${plat}/*.sh  ${cafloc_path}/c.SYSKIT/${plat}/
		cp ${SYSKIT_TOPDIR}/${plat}/*.cmd ${cafloc_path}/c.SYSKIT/${plat}/
	done

	mkdir -p ${cafloc_path}/d.MIDKIT
	cp ${MIDKIT_TOPDIR}/*.sh  ${cafloc_path}/d.MIDKIT/

	mkdir -p ${cafloc_path}/e.APPKIT
	cp ${APPKIT_TOPDIR}/*.sh  ${cafloc_path}/e.APPKIT/ 
	cp ${APPKIT_TOPDIR}/src   ${cafloc_path}/e.APPKIT/ -rf
	cp ${APPKIT_TOPDIR}/pkg   ${cafloc_path}/e.APPKIT/ -rf
	
	mkdir -p ${cafloc_path}/f.RCSKIT
	cp ${RCSKIT_TOPDIR}/*.sh    ${cafloc_path}/f.RCSKIT/
	cp ${RCSKIT_TOPDIR}/src     ${cafloc_path}/f.RCSKIT/ -rf
	cp ${RCSKIT_TOPDIR}/pkg     ${cafloc_path}/f.RCSKIT/ -rf

	mkdir -p ${cafloc_path}/k.KNFKIT
	cp ${KNFKIT_TOPDIR}/*.sh    ${cafloc_path}/k.KNFKIT/
	cp ${KNFKIT_TOPDIR}/src     ${cafloc_path}/k.KNFKIT/ -rf
	cp ${KNFKIT_TOPDIR}/pkg     ${cafloc_path}/k.KNFKIT/ -rf

	mkdir -p ${cafloc_path}/m.MGRKIT
	cp ${MGRKIT_TOPDIR}/*.sh    ${cafloc_path}/m.MGRKIT/
	cp ${MGRKIT_TOPDIR}/src     ${cafloc_path}/m.MGRKIT/ -rf
	cp ${MGRKIT_TOPDIR}/pkg     ${cafloc_path}/m.MGRKIT/ -rf
	
	mkdir -p ${cafloc_path}/o.OTAKIT
	cp ${OTAKIT_TOPDIR}/*       ${cafloc_path}/o.OTAKIT/ -rf
	
	mkdir -p ${cafloc_path}/w.DEVKIT
	echo "empty" > ${cafloc_path}/w.DEVKIT/readme.txt

	mkdir -p ${cafloc_path}/x.INFKIT
	echo "empty" > ${cafloc_path}/x.INFKIT/readme.txt

	mkdir -p ${cafloc_path}/y.IMGBINS
	echo "empty" > ${cafloc_path}/y.IMGBINS/readme.txt

	msglog "聚集cafloc [DONE]"
	msglog "--------------------------------------------------"
}

clone_cafgit()
{
	msglog "--------------------------------------------------1"
	if [ -d ${cafgit_path} ]; then
		msglog "已存在目录: ${cafgit_path} [ERROR]"
		return
	fi
	msglog "克隆目录: ${cafgit_path} [START]"
	mkdir -p  ${cafgit_root} 
	cd ${cafgit_root}
	git clone  ${cafgit_site}
	if [ $? -ne 0 ]; then
		msglog "克隆目录: ${cafgit_path} [FAILED]"
	fi
	msglog "克隆目录: ${cafgit_path} [DONE]"
	msglog "--------------------------------------------------"
}

pull_cafgit()
{
	msglog "--------------------------------------------------1"
	if [ ! -d ${cafgit_path} ]; then
		msglog "不存在目录: ${cafgit_path} [ERROR]"
		return
	fi
	msglog "同步目录: ${cafgit_path} [START]"
	cd ${cafgit_path}
	git pull 
	if [ $? -ne 0 ]; then
		msglog "同步目录: ${cafgit_path} [FAILED]"
	fi
	msglog "同步目录: ${cafgit_path} [DONE]"
	msglog "--------------------------------------------------"
}

commit_cafgit()
{
	msglog "--------------------------------------------------1"
	if [ ! -d ${cafgit_path} ]; then
		msglog "不存在目录: ${cafgit_path} [ERROR]"
		return
	fi
	cd ${cafgit_path}
	local curdate=$(date +%y%m%d%H%M)
	msglog "查看当前git状态:"
	git status .
	idx=10
	while [ ${idx} -ne 0 ]
	do
		msglog "将会在${idx}秒后执行递交 [TMO]"
		sleep 1
		let idx--
	done
	msglog "递交目录: ${cafgit_path} [START]"
	git add -A .
	git commit -m "cfggit commit at ${curdate}"
	msglog "递交目录: ${cafgit_path} [DONE]"
	msglog "--------------------------------------------------"
}

push_cafgit()
{
	msglog "--------------------------------------------------"
	if [ ! -d ${cafgit_path} ]; then
		msglog "不存在目录: ${cafgit_path} [ERROR]"
		return
	fi
	msglog "推送目录: ${cafgit_path} [START]"

	cd ${cafgit_path}
	git push origin master
	if [ $? -ne 0 ]; then
		msglog "推送目录: ${cafgit_path} [FAILED]"
	fi
	msglog "推送目录: ${cafgit_path} [DONE]"
	msglog "--------------------------------------------------"
}
update_cafgit()
{
	read -p "这将覆本地dugicaf内容，请再次确认(y/n): " choice
	if [ ! "x${choice}" == "xy" ]; then
		msglog "操作已经终止 [ERROR]"
		return 1
	fi
	msglog "--------------------------------------------------"
	msglog "拷贝: ${cafloc_path} 到 ${cafgit_path} [START]"
	echo "cp -rf ${cafloc_path}/* ${cafgit_path}/"
	cp -rf ${cafloc_path}/* ${cafgit_path}/
	msglog "拷贝: ${cafloc_path} 到 ${cafgit_path} [DONE]"
	msglog "--------------------------------------------------"
}

show_cafnote()
{
	msglog "--------------------------------------------------"
	msglog "CAF框架目录说明:"
	msglog "a.OEMKIT   存放设备配置和基础脚本"
	msglog "b.GCCKIT   存放编译工具和编译依赖"
	msglog "c.SYSKIT   存放系统源码"
	msglog "d.MIDKIT   存放第三方包"
	msglog "e.APPKIT   存放应用程序"
	msglog "f.RCSKIT   存放加密系统程序"
	msglog "k.KNFKIT   存放文件系统程序"
	msglog "m.MGRKIT   存放量产相关程序"
	msglog "o.OTAKIT   存放远程升级内容"
	msglog "w.DEVKIT   存放二次开发包"
	msglog "x.INFKIT   存放临时文件"
	msglog "y.IMGBINS  存放烧写文件和版本文件"
	msglog "z.out.boot 指向引导程序输出目录"
	msglog "z.out.kern 指向内核程序输出目录"
	msglog "z.out.knfs 指向内存文件系统副本目录"
	msglog "z.out.rtfs 指向用户文件系统副本目录"
	msglog "z.src.boot 指向引导程序源码目录"
	msglog "z.src.kern 指向内核程序源码目录"
	msglog "z.src.knfs 指向内存文件系统原始目录"
	msglog "z.src.rtfs 指向用户文件系统原始目录"
	msglog "--------------------------------------------------"
}

main_proc()
{
	if [ -z "$1" ]; then
		show_usage
		return 1
	fi
	
	if [ "x$1" = "x01" ]; then
		show_cafnote
	elif [ "x$1" = "x02" ]; then
		clone_cafgit
	elif [ "x$1" = "x03" ]; then
		pull_cafgit
	elif [ "x$1" = "x04" ]; then
		commit_cafgit
	elif [ "x$1" = "x05" ]; then
		push_cafgit
	elif [ "x$1" = "x06" ]; then
		gether_cafloc
	elif [ "x$1" = "x07" ]; then
		update_cafgit
	fi
	show_end
}

main_proc $@

